package cn.com.miao.lynx.plugin.sms;

import cn.com.miao.lynx.plugin.sms.properties.SmsProperties;
import cn.com.miao.lynx.plugin.sms.service.impl.AliyunSmsService;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static cn.com.miao.lynx.plugin.sms.properties.SmsProperties.SMS_PREFIX;

/**
 * SmsAutoConfiguration
 *
 * @title: SmsAutoConfiguration
 * @description:
 * @author: dengmiao
 * @create: 2020-01-10 10:57
 **/
@Slf4j
@AllArgsConstructor
@Configuration
@ConditionalOnClass(SendSmsRequest.class)
@ConditionalOnProperty(prefix = SMS_PREFIX, name = {"access-key-id", "access-key-secret", "sign-name"})
@EnableConfigurationProperties(SmsProperties.class)
public class SmsAutoConfig {

    private final SmsProperties smsProperties;

    @Bean
    @ConditionalOnMissingBean
    public AliyunSmsService apiBootSmsService() {
        return AliyunSmsService.builder()
                .accessKeyId(smsProperties.getAccessKeyId())
                .accessKeySecret(smsProperties.getAccessKeySecret())
                .signName(smsProperties.getSignName())
                .profile(smsProperties.getProfile())
                .connectionTimeout(smsProperties.getConnectionTimeout())
                .readTimeout(smsProperties.getReadTimeout())
                .build();
    }
}
